package cn.zcscloud.mall.service.impl;

import cn.zcscloud.mall.mbg.mapper.SmsFlashPromotionMapper;
import cn.zcscloud.mall.mbg.mapper.SmsFlashPromotionSessionMapper;
import cn.zcscloud.mall.mbg.model.SmsFlashPromotion;
import cn.zcscloud.mall.mbg.model.SmsFlashPromotionProductRelation;
import cn.zcscloud.mall.mbg.model.SmsFlashPromotionSession;
import cn.zcscloud.mall.mbg.model.SmsFlashPromotionSessionExample;
import cn.zcscloud.mall.service.SmsFlashPromotionProductRelationService;
import cn.zcscloud.mall.service.SmsFlashPromotionService;
import cn.zcscloud.mall.service.SmsFlashPromotionSessionService;
import cn.zcscloud.mall.vo.SmsFlashPromotionSessionDetail;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SmsFlashPromotionSessionServiceImpl extends ServiceImpl<SmsFlashPromotionSessionMapper, SmsFlashPromotionSession> implements SmsFlashPromotionSessionService {
    @Autowired
    private SmsFlashPromotionSessionMapper smsFlashPromotionSessionMapper;
    @Autowired
    private SmsFlashPromotionProductRelationService relationService;

    @Override
    public List<SmsFlashPromotionSessionDetail> selectList(Long flashPromotionId) {
        //创建vo容器
        List<SmsFlashPromotionSessionDetail> result = new ArrayList<>();
        //统计上线活动数
        LambdaQueryWrapper<SmsFlashPromotionSession> promotionSessionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        promotionSessionLambdaQueryWrapper.eq(SmsFlashPromotionSession::getStatus,1);
        List<SmsFlashPromotionSession> sessionList = smsFlashPromotionSessionMapper.selectList(promotionSessionLambdaQueryWrapper);
        for (SmsFlashPromotionSession promotionSession : sessionList) {
            //创建vo对象
            SmsFlashPromotionSessionDetail detail = new SmsFlashPromotionSessionDetail();
            BeanUtils.copyProperties(promotionSession, detail);
            LambdaQueryWrapper<SmsFlashPromotionProductRelation> relationLambdaQueryWrapper = new LambdaQueryWrapper<>();
            relationLambdaQueryWrapper.eq(SmsFlashPromotionProductRelation::getFlashPromotionSessionId,promotionSession.getId());
            Integer count = relationService.count(relationLambdaQueryWrapper);
            detail.setProductCount(count);
            result.add(detail);
        }
        return result;
    }
}
